package org.sky.ajax;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.sky.action.MyActionSupport;
import org.sky.model.Evaluate;
import org.sky.model.FileUtil;
import org.sky.model.Person;
import org.sky.service.EvaluateService;
import org.sky.service.FileUtilService;
import org.sky.service.PersonService;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-11-13
 * Time: 下午1:29
 * To change this template use File | Settings | File Templates.
 */
@Component("evaluateAjax")
@Scope("prototype")
public class EvaluateAjax extends MyActionSupport{
    private static Logger logger = LogManager.getLogger(EvaluateAjax.class.getName());
    private PersonService personService;
    private EvaluateService evaluateService;
    private FileUtilService fileUtilService;
    private HttpServletRequest request = ServletActionContext.getRequest();
    private HttpServletResponse response = ServletActionContext.getResponse();
    @Override
    public String execute(){

            UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
                    .getAuthentication()
                    .getPrincipal();
            String rate = this.request.getParameter("rate");
            String fileId = this.request.getParameter("fileId");
            String userName = userDetails.getUsername();

            Map<String, Object> params = new LinkedHashMap<String, Object>();
            params.put("userName",userName);
            Evaluate evaluate = new Evaluate();
            Person person = (Person) personService.findSpecialObject(Person.class, params);
        try {
            FileUtil fileUtil = fileUtilService.find(FileUtil.class,Integer.valueOf(fileId));
            evaluate.setFile(fileUtil);
        } catch (Exception e) {
            logger.trace(e);
            return ERROR;
        }
            evaluate.setStar(Integer.valueOf(rate));
            evaluate.setUser(person);
            evaluateService.save(evaluate);
            this.sendResponse(evaluate.getId());
            return NONE;

    }

    public String addEvaluate(){
        String myEvaluateId = this.request.getParameter("myEvaluateId");
        if(!myEvaluateId.equals("")){
            response.setContentType("text/html;charset=utf-8");
            String old = this.request.getParameter("comment");
            try {
                String comment = URLDecoder.decode(old, "UTF-8");
                Evaluate evaluate = evaluateService.find(Evaluate.class,Integer.valueOf(myEvaluateId));
                evaluate.setEvaluate(comment);
                evaluateService.update(evaluate);
            } catch (UnsupportedEncodingException e) {
               logger.trace(e);
            }
        }
        return NONE;
    }

    @Resource(name = "personServiceImpl")
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }

    @Resource(name = "evaluateServiceImpl")
    public void setEvaluateService(EvaluateService evaluateService) {
        this.evaluateService = evaluateService;
    }

    @Resource(name = "fileUtilServiceImpl")
    public void setFileUtilService(FileUtilService fileUtilService) {
        this.fileUtilService = fileUtilService;
    }
}
